下記の2つの散布図を講義に含めていただけるとありがたいです。
library(tidyverse)
── Attaching core tidyverse packages ──────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.2 ✔ readr 2.1.4
✔ forcats 1.0.0 ✔ stringr 1.5.0
✔ ggplot2 3.4.2 ✔ tibble 3.2.1
✔ lubridate 1.9.2 ✔ tidyr 1.3.0
✔ purrr 1.0.1 ── Conflicts ────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
library(WDI)
Macroeonomics 7eds, Chapter 4: Money and Inflation Figure 4-1 and Figure 4-2
Scatter plots between Inflation rate (percent) and Money supply growth (percent)
Indicator1: Broad money (current LCU)-a proxy to the money supply
FM.LBL.BMNY.CN
Broad money (IFS line 35L..ZK) is the sum of currency outside banks; demand deposits other than those of the central government; (図を描くために、変化率を計算する必要があります。)
Indicator2: Inflation, consumer prices (annual %)
FP.CPI.TOTL.ZG
Inflation as measured by the consumer price index reflects the annual percentage change in the cost to the average consumer of acquiring a basket of goods and services that may be fixed or changed at specified intervals, such as yearly. The Laspeyres formula is generally used.
df1 <- WDI("all", c(bmny = "FM.LBL.BMNY.CN", cpi = "FP.CPI.TOTL.ZG"), extra = TRUE)
glimpse(df1)
Rows: 16,758
Columns: 14
$ country <chr> "Afghanistan", "Afghanistan", "Afghanistan", …
$ iso2c <chr> "AF", "AF", "AF", "AF", "AF", "AF", "AF", "AF…
$ iso3c <chr> "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AF…
$ year <int> 2014, 2012, 2009, 2013, 1971, 2015, 1969, 201…
$ status <chr> "", "", "", "", "", "", "", "", "", "", "", "…
$ lastupdated <chr> "2023-05-10", "2023-05-10", "2023-05-10", "20…
$ bmny <dbl> 402465467444, 338614163696, 202231264210, 372…
$ cpi <dbl> 4.6739960, 6.4412128, -6.8111611, 7.3857718, …
$ region <chr> "South Asia", "South Asia", "South Asia", "So…
$ capital <chr> "Kabul", "Kabul", "Kabul", "Kabul", "Kabul", …
$ longitude <chr> "69.1761", "69.1761", "69.1761", "69.1761", "…
$ latitude <chr> "34.5228", "34.5228", "34.5228", "34.5228", "…
$ income <chr> "Low income", "Low income", "Low income", "Lo…
$ lending <chr> "IDA", "IDA", "IDA", "IDA", "IDA", "IDA", "ID…
df1 %>% filter(iso3c == "USA") %>%
filter(year %in% seq(1960, 2020, 5)) %>%
ggplot(aes(x = bmny, y = cpi, label = year)) + geom_point() + geom_label(nudge_y = 1)
df1 %>% filter(iso3c == "USA") %>%
filter(year %in% seq(1960, 2020, 5)) %>%
mutate(mnyp = bmny / lag(bmny)) %>% drop_na(mnyp) %>%
ggplot(aes(x = mnyp, y = cpi, label = year)) + geom_point() + geom_label(nudge_x = 0.05) + labs(title = "Change in 5 year span")
library(plotly)
ppp1 <- df1 %>% filter(iso3c == "USA") %>%
filter(year %in% seq(1960, 2020, 5)) %>%
mutate(mnyp = bmny / lag(bmny)) %>% drop_na(mnyp) %>%
ggplot(aes(x = mnyp, y = cpi, label = year)) + geom_point()
ggplotly(ppp1)
ppp2 <- df1 %>% filter(region != "Aggregates") %>%
filter(country != "Angola") %>%
filter(year %in% c(1999, 2007)) %>%
group_by(country) %>%
mutate(mnyp = bmny / lag(bmny)) %>% drop_na(mnyp) %>%
ggplot(aes(x = mnyp, y = cpi, label = country, col = income)) + geom_point() +
scale_x_log10()
ggplotly(ppp2)
Macroeonomics 7eds, Chapter 9: Introduction to Economic Fluctuations, Figure 9-4
Indicator1: GDP growth (annual %)
NY.GDP.MKTP.KD.ZG
Annual percentage growth rate of GDP at market prices based on constant local currency. Aggregates are based on constant 2015 prices, expressed in U.S. dollars. GDP is the sum of gross value added by all resident producers in the economy plus any product taxes and minus any subsidies not included in the value of the products. It is calculated without making deductions for depreciation of fabricated assets or for depletion and degradation of natural resources.
Indicator2: Unemployment, total (% of total labor force) (modeled ILO estimate)
SL.UEM.TOTL.ZS
Unemployment refers to the share of the labor force that is without work but available for and seeking employment.
df2 <- WDI("all", c(growth = "NY.GDP.MKTP.KD.ZG", unemploy = "SL.UEM.TOTL.ZS"), extra = TRUE)
glimpse(df2)
Rows: 16,758
Columns: 14
$ country <chr> "Afghanistan", "Afghanistan", "Afghanistan", …
$ iso2c <chr> "AF", "AF", "AF", "AF", "AF", "AF", "AF", "AF…
$ iso3c <chr> "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AF…
$ year <int> 2014, 2012, 2009, 2013, 1971, 2015, 1969, 201…
$ status <chr> "", "", "", "", "", "", "", "", "", "", "", "…
$ lastupdated <chr> "2023-05-10", "2023-05-10", "2023-05-10", "20…
$ growth <dbl> 2.7245434, 12.7522871, 21.3905284, 5.6007447,…
$ unemploy <dbl> 7.910, 8.019, 8.082, 7.949, NA, 8.989, NA, 8.…
$ region <chr> "South Asia", "South Asia", "South Asia", "So…
$ capital <chr> "Kabul", "Kabul", "Kabul", "Kabul", "Kabul", …
$ longitude <chr> "69.1761", "69.1761", "69.1761", "69.1761", "…
$ latitude <chr> "34.5228", "34.5228", "34.5228", "34.5228", "…
$ income <chr> "Low income", "Low income", "Low income", "Lo…
$ lending <chr> "IDA", "IDA", "IDA", "IDA", "IDA", "IDA", "ID…
df2p <- df2 %>% filter(iso3c == "USA") %>%
mutate(unemployp = unemploy/lag(unemploy)) %>%
drop_na(unemployp)
df2p
df2p %>%
ggplot(aes(x = unemployp, y = growth, label = year)) + geom_point() + geom_label(nudge_y = 1)
ppp3 <- df2p %>%
ggplot(aes(x = unemployp, y = growth, label = year)) + geom_point() + geom_label(nudge_y = 1) + geom_smooth(method = "lm", formula = 'y~x', se = FALSE)
ggplotly(ppp3)
Warning: Removed 1 rows containing non-finite values (`stat_smooth()`).Warning: The following aesthetics were dropped during statistical
transformation: label
ℹ This can happen when ggplot fails to infer the correct grouping
structure in the data.
ℹ Did you forget to specify a `group` aesthetic or to convert a
numerical variable into a factor?Warning: geom_GeomLabel() has yet to be implemented in plotly.
If you'd like to see this geom implemented,
Please open an issue with your example code at
https://github.com/ropensci/plotly/issues
df2p %>% lm(growth ~ unemployp, .) %>% summary()
Call:
lm(formula = growth ~ unemployp, data = .)
Residuals:
Min 1Q Median 3Q Max
-1.9373 -0.8906 -0.2653 0.9466 1.8099
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 8.1191 0.7729 10.504 4.89e-11 ***
unemployp -5.4873 0.7370 -7.445 5.22e-08 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 1.091 on 27 degrees of freedom
(1 observation deleted due to missingness)
Multiple R-squared: 0.6724, Adjusted R-squared: 0.6603
F-statistic: 55.43 on 1 and 27 DF, p-value: 5.221e-08